package com.briup.product_source.service.impl;

import com.briup.product_source.dao.DiseaseRecordMapper;
import com.briup.product_source.dao.ManagerAnimalMapper;
import com.briup.product_source.dao.ManagerDiseaseMapper;
import com.briup.product_source.dao.ext.DiseaseRecordExtMapper;
import com.briup.product_source.exception.ServiceException;
import com.briup.product_source.pojo.DiseaseRecord;
import com.briup.product_source.pojo.ManagerAnimal;
import com.briup.product_source.pojo.ManagerDisease;
import com.briup.product_source.pojo.ext.DiseaseRecordExt;
import com.briup.product_source.result.ResultCode;
import com.briup.product_source.service.DiseaseRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DiseaseRecordServiceImpl implements DiseaseRecordService {


    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;

    @Autowired
    private DiseaseRecordExtMapper diseaseRecordExtMapper;

    @Autowired
    private ManagerDiseaseMapper managerDiseaseMapper;

    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;

    @Override
    public PageInfo<DiseaseRecordExt> queryAllDisease(Integer pageNum, Integer pageSize, String drStatus,Integer drDId) {
        PageHelper.startPage(pageNum,pageSize);
        List<DiseaseRecordExt> list = diseaseRecordExtMapper.selectAll(drStatus,drDId);
        System.out.println(list.size());
        PageInfo<DiseaseRecordExt> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<ManagerDisease> queryAllAndSelect() {
        return managerDiseaseMapper.selectAll();
    }

    @Transactional
    @Override
    public void saveOrUpdate(DiseaseRecord diseaseRecord) {
        String drAnimalId = diseaseRecord.getDrAnimalId();
        int count;
        Integer id = diseaseRecord.getDrId();
        String status = diseaseRecord.getDrStatus();
        if (!StringUtils.hasText(drAnimalId) ||
                !StringUtils.hasText(diseaseRecord.getDrDesc()) ||
                diseaseRecord.getDrDId() == null ||
                diseaseRecord.getDrTime() == null ) {
            throw new ServiceException(ResultCode.PARAM_IS_EMPTY);
        }
        ManagerAnimal managerAnimal = managerAnimalMapper.selectByPrimaryKey(drAnimalId);
        if (managerAnimal == null) {
            throw new ServiceException(ResultCode.FAIL);
        } else if (!"养殖中".equals(managerAnimal.getAStatus()) && !"已检疫".equals(managerAnimal.getAStatus())){
            throw new ServiceException(ResultCode.ANIMAL_IS_NOT_IN_BREEDING);
        }
        if (id != null) {
            // 有id->更新操作
            if (diseaseRecordMapper.selectByPrimaryKey(id) != null) {
                count = diseaseRecordMapper.updateByPrimaryKey(diseaseRecord);
            }else {
                throw new ServiceException(ResultCode.DATA_IS_EMPTY);
            }
        }else {
            //添加
            if (!StringUtils.hasText(status)) {
                diseaseRecord.setDrStatus("未治疗");
            }
            count = diseaseRecordMapper.add(diseaseRecord);
        }
        if(count == 0){
            throw new ServiceException(ResultCode.FAIL);
        }
        String aHealthy = "健康";
        if (!"已治疗".equals(status)) {
            aHealthy = "生病";
        }
        if (managerAnimalMapper.updateAHealthyByAAnimalId(aHealthy, drAnimalId) == 0) {
            throw new ServiceException(ResultCode.FAIL);
        }
    }

    @Override
    public void deleteById(Integer id) {
        DiseaseRecord dr = diseaseRecordMapper.selectByPrimaryKey(id);
        String aId = dr.getDrAnimalId();
        if(dr == null){
            throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }else {
            ManagerAnimal ma = managerAnimalMapper.selectByPrimaryKey(aId);
            if(ma != null){
                if(!ma.getAStatus().equals("已销售")){
                    throw new ServiceException(ResultCode.DISEASE_IS_NOT_IN_BREEDING);
                }else {
                    if(diseaseRecordMapper.deleteById(id) == 0){
                        throw new ServiceException(ResultCode.FAIL);
                    }
                }
            } else {
                    if(diseaseRecordMapper.deleteById(id) == 0){
                        throw new ServiceException(ResultCode.FAIL);
                    }
            }
        }
    }

    @Override
    public void deleteByChoice(List<Integer> ids) {
        List<Integer> result = new ArrayList<>();
        int empty = 0;
        int rel = 0;
        for (Integer id : ids) {
            DiseaseRecord dr = diseaseRecordMapper.selectByPrimaryKey(id);
            String aId = dr.getDrAnimalId();
            if(dr == null){
                empty++;
            }else {
                ManagerAnimal ma = managerAnimalMapper.selectByPrimaryKey(aId);
                if(ma != null){
                    if(!ma.getAStatus().equals("已销售")){
                       rel++;
                    }else{
                        result.add(id);
                    }
                } else {
                    result.add(id);
                }
            }
        }
        if (empty == ids.size()){
             throw new ServiceException(ResultCode.DATA_IS_EMPTY);
        }
        if (rel == ids.size()){
            throw new ServiceException(ResultCode.DISEASE_IS_NOT_IN_BREEDING);
        }
        if(diseaseRecordMapper.deleteByChoice(result) == 0){
            throw new ServiceException(ResultCode.FAIL);
        }
    }
}
